import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/pay_notice_bean.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/receipt_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

///开票审核，已开发票
class UiReceiptDetailsInfo {
  static Widget build(BuildContext context, List<ReceiptDetail>? data,
      {String? title}) {
    if (StringUtils.isEmpty(data)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).HaveInvoice; //已开发票
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(
      context,
      data,
    ));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(
    BuildContext context,
    List<ReceiptDetail>? data,
  ) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context)));
        if (data != null && data.length > 0) {
          var postion = 1;
          data.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, postion, element, setState);
            postion++;
            children.add(TableRow(children: cells));
          });
        }

        ///合计
        if (data != null) {
          children.add(TableRow(
              //最后一行，添加背景色
              decoration:
                  BoxDecoration(color: AppColors.backgroundColorWithTableAcom),
              children: _aCombined(context, data!)));
        }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {
            0: FixedColumnWidth(50),
            1: FixedColumnWidth(150),
            6: FixedColumnWidth(180),
          },
          border: AppStyle.getTableBorder(),
          textDirection: TextDirection.ltr,
          children: children,
        );
        var width = 1000.0;
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).ReceiptCode3)); //发票号
    children.add(_addItem(AppStrings.of(context).AddPersonName)); //开票人
    children.add(_addItem(AppStrings.of(context).ReceiptAmount)); //开票金额
    children.add(_addItem(AppStrings.of(context).ReceiptAmountNoTax)); //无税金额
    children.add(_addItem(AppStrings.of(context).ReceiptAmountTax)); //税额
    children.add(_addItem(AppStrings.of(context).AddDate8)); //开票时间
    children.add(_addItem(AppStrings.of(context).StatusName2)); //发票状态
    return children;
  }

  ///内容
  static List<Widget> _contents(BuildContext context, int postion,
      ReceiptDetail element, StateSetter setState) {
    List<Widget> children = [];
    children.add(_addItem((postion).toString())); //序号
    children.add(_addItem(element.ReceiptCode)); //发票号
    children.add(_addItem(element.AddPersonName)); //开票人
    children.add(_addItem(element.ReceiptAmount)); //开票金额
    children.add(_addItem(element.ReceiptAmountNoTax)); //无税金额
    children.add(_addItem(element.ReceiptAmountTax)); //税额
    children.add(_addItem(element.AddDate)); //开票时间
    children.add(_addItem(element.StatusName)); //发票状态
    return children;
  }

  ///合计
  static List<Widget> _aCombined(
      BuildContext context, List<ReceiptDetail> datas) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).ACombined)); //合计
    children.add(_addItem("")); //发票号
    children.add(_addItem("")); //开票人
    double ReceiptAmount = 0;
    datas?.forEach((element) {
      var str = element.ReceiptAmount;
      ReceiptAmount = AcombinedUtils.add(ReceiptAmount, str);
    });
    children.add(_addItem(AcombinedUtils.getMoney(ReceiptAmount))); //fixme 开票金额
    double ReceiptAmountNoTax = 0;
    datas?.forEach((element) {
      var str = element.ReceiptAmountNoTax;
      ReceiptAmountNoTax = AcombinedUtils.add(ReceiptAmountNoTax, str);
    });
    children.add(
        _addItem(AcombinedUtils.getMoney(ReceiptAmountNoTax))); //fixme 无税金额
    double ReceiptAmountTax = 0;
    datas?.forEach((element) {
      var str = element.ReceiptAmountTax;
      ReceiptAmountTax = AcombinedUtils.add(ReceiptAmountTax, str);
    });
    children
        .add(_addItem(AcombinedUtils.getMoney(ReceiptAmountTax))); //fixme 税额
    children.add(_addItem("")); //开票时间
    children.add(_addItem("")); //发票状态
    return children;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false, GestureTapCallback? onTap}) {
    Widget child = Center(child: _text(text, isBlue: isBlue));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text, {bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    return Text(
      text?.trim() ?? "",
      style: style,
    );
  }
}
